﻿using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using METU_CMT.DataSets.METUCMTDataSetTableAdapters;

namespace METU_CMT.Business
{
    public class Permitter
    {
        public bool IsUserPermitted(string username, string url)
        {
            List<string> _permittedUrls;
            if (HttpContext.Current.Session["Permissions"] == null)
            {
                PermissionsTableAdapter _adapter = new PermissionsTableAdapter();
                DataTable _permissions = _adapter.GetDataByUserName(username);

                _permittedUrls = (from DataRow _permittedUrlDr in _permissions.Rows select _permittedUrlDr["Url"].ToString()).ToList();
                HttpContext.Current.Session["Permissions"] = _permittedUrls;
            }

            _permittedUrls = HttpContext.Current.Session["Permissions"] as List<string>;
            return _permittedUrls != null && _permittedUrls.Any(url.Contains);
        }
    }
}